package com.maozai.maozaimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.maozai.maozaimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2021000118657060";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCzQVxH4CHmqKEVaJka3bYv9Oo74MvF0ETKWaphWWOQGPng84OLkIEgtyxXlKoO65tG1Puf+e7op4Ey4Yfd2WDf+9gpgNHgRtihwlA8sm12O42yZJX1do9lfmiA6zyhdAyZ+r5h6rIQGbGFvq8e2fgir1icTbPqv0Pize5fhKSErnXpQ6h8hIvTCJX0si/nWFCXJSLNtrXWj/xg+I85/ecblNWkJZCHGgzk98LqqccpkOGXteify959QwPyx1HjQMZx6mxsymUVOOi1PtWrKf2FTOCNwsHxzTI9H3lbxq6x7Zv3oLwgaIFfwZguG4MEDP9S7IDsO8vYOKYLcOL/NUnhAgMBAAECggEBAKeTe8SJyMeMb4LHFRKjoFU23d76aSVVbWnJ51k4qrYkUz20CQ7pGX5PH3oEtfJa2f+ezlkg2vLO3R6G7nA8bq8WDl/GWO4U7SJTuJT1rRUx2WajV/lwS9/zC5kx0K2TAtDUMzTwpmQc0POabrYlyS4LxIfisEbNZZZbM/RB2Vda9+nxuXeW2oxeBYzhTaCeIpXXLutcW1Ble6IRFppzjbav9rc34q4ukFOhU5imYk0UvTvYUgndzwGFR1feXUr2A7rbanLg1sPOkE8duWL5AMH3YCau1rAhD5qQgd209O2iN99NTfifldtD4n6pHc35oH2XdRcHI8CsRyHmCcIPkn0CgYEA+j1lCe39nfjUHRPVwDvwK8sHUZEXA/tFUon2qQHzA3gIDPeqSf2/TJTpLwUoWFpiiDv332cvp+pNQaqhXPtNkDMtYGeBU8kPViVdSsn9rilk8ZGw7NlCrOr4pz5FMamF4T0YZjDyetuw662625bfer4j761VDGUO3/VPa9uIcDMCgYEAt2Grpz8fe6vuPc7rZJN+oIVTkdbguZzhF07t+UZE+iJxIRwI6YqD3FYvGPaEHZho9WdVzSSSMrDgWnbBdvZrLh5Zz+VdfCEDuDgq0DniPXRdsIq6z5J130E+yEL5fHQoceed+pQVsrOKo5uEoe5z19gJGbPyVvkekWqkmkzqOZsCgYBxs4e/VUXRi4hFlgefixx7qYqvwy8j/f3nnQxYiqwyvU1bJwVXDVbooxi45q0ISDlREX51IduDfnCQD8m2B/GcPcS2cxFOUqMHT37uRo1fnytgoAQ1+hnTnAhTZXXnNNHCIBKADGmA7WWcEJpAwHLWwKJveuCT+y8hD8OAjIWYBQKBgQCwkiPUusvWqbqQ2u7+faJcb2QDNdJWBn8ZkU9ESOvbqVQinDrcnBnRC074C000re7J9EDCcScwFe1ATb6EjEqxMlop+mCkR6msNLDMpB96KtSXS00K1qwWVl1cOFi/A51555nOj83Arm2ckP8JaG80cnh4vjJmmiJbxEJPrCWPZwKBgCWT9s9DUnopeoA/Z471vN+2/iQ3DivgOW6NRIU4Z/IoCO5d99MYou3nqKwfBMHuULqrT3BTvQOxP1m9itI6tlycrBTwseYPNA485eyYEAXp4Za1h2LusUlUHmbZdqfi4b2kVfdUi9+IYzKXGrBAJR6LgGh9YqCume4h3D78K7GB";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApcRVM1DSOYgOIe3LL+6MV0B70rrXT30KbRK8k6xKvB61ysYj+x8wdrGu/g536YFACPrWzIUKS0rqXeTyXb/5AbZAyteW44m+dbgcZwr4O0xmtQg1XFxixu4izza485KIM5BfeKmv/RZi++41bpstv+z/nS4BtSyrR1Oic4m3vjydna9dZueGF0w6wknCcJYBUNqm0hE/eeJCzspcxVy00zJEbtbm9y3C/GTm3aPX8/400thdf8C2ZlM19K/s547/bgjhowAI0X327mADCwioSw2XAGzY3rGrJcsGS5wZvds2C25hUjevU/oyYIhR1mVJnSqnSvf2AlYEiDXgOPKf1wIDAQAB";
    // MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApcRVM1DSOYgOIe3LL+6MV0B70rrXT30KbRK8k6xKvB61ysYj+x8wdrGu/g536YFACPrWzIUKS0rqXeTyXb/5AbZAyteW44m+dbgcZwr4O0xmtQg1XFxixu4izza485KIM5BfeKmv/RZi++41bpstv+z/nS4BtSyrR1Oic4m3vjydna9dZueGF0w6wknCcJYBUNqm0hE/eeJCzspcxVy00zJEbtbm9y3C/GTm3aPX8/400thdf8C2ZlM19K/s547/bgjhowAI0X327mADCwioSw2XAGzY3rGrJcsGS5wZvds2C25hUjevU/oyYIhR1mVJnSqnSvf2AlYEiDXgOPKf1wIDAQAB
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://maozai.free.idcfengye.com/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";


    private String timeout = "30m";


    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();


        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        /*System.out.println("参数格式：==> {\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");*/
        //System.out.println("总金额：==> " + total_amount);
        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        //System.out.println("支付宝的响应："+result);

        return result;

    }
}
